﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Arizu.Repository;
using Arizu.Entity;

namespace AiRiZu
{
    public partial class SendRoom : System.Web.UI.Page
    {
        Arizu.Entity.SendRoom sr = new Arizu.Entity.SendRoom();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                init();
            }

        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        protected void init()
        {
            this.sheng.Items.Clear();
            this.city.Items.Clear();
            this.quyu.Items.Clear();
            DataTable dt = new DataTable();
            dt = Main.CreateInstance().GetShengList();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.sheng.Items.Add(new ListItem(dt.Rows[i]["provinceName"].ToString(), dt.Rows[i]["provinceID"].ToString()));
                }
                this.sheng.Items.Insert(0, new ListItem("请选择", "0"));
            }
            this.city.Items.Insert(0, new ListItem("请选择", "0"));
            this.quyu.Items.Insert(0, new ListItem("请选择", "0"));
        }

        /// <summary>
        /// 市根据省联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sheng_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.sheng.SelectedValue != "0")
            {
                this.city.Items.Clear();
                DataTable dt = new DataTable();
                dt = Main.CreateInstance().GetCityList(this.sheng.SelectedValue);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        this.city.Items.Add(new ListItem(dt.Rows[i]["cityName"].ToString(), dt.Rows[i]["cityID"].ToString()));
                    }
                    this.city.Items.Insert(0, new ListItem("请选择", "0"));
                }
            }
            else
            {
                this.city.Items.Clear();
                this.city.Items.Insert(0, new ListItem("请选择", "0"));
                this.quyu.Items.Clear();
                this.quyu.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

        /// <summary>
        /// 区域根据市联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void city_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.city.SelectedValue != "0")
            {
                this.quyu.Items.Clear();
                DataTable dt = new DataTable();
                dt = Main.CreateInstance().GetAreaList(this.city.SelectedValue);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        this.quyu.Items.Add(new ListItem(dt.Rows[i]["areaName"].ToString(), dt.Rows[i]["areaID"].ToString()));
                    }
                    this.quyu.Items.Insert(0, new ListItem("请选择", "0"));
                }
            }
            else
            {
                this.quyu.Items.Clear();
                this.quyu.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

        protected void sendinfo_Click(object sender, EventArgs e)
        {
            if (Session["logID"] == "" || Session["logID"] == null)
            {
                Response.Redirect("User/Login.aspx");
            }
            else
            {
                int userid = 0;
                string xingmingtxt = "";
                string dianhuatxt = "";
                string emailtxt = "";
                string shengtxt = "";
                string citytxt = "";
                string quyutxt = "";
                string adressdesctxt = "";
                string emailbody = "";
                string emailtitle = "";
                userid = int.Parse(Session["logId"].ToString());
                sr.OwnerID = userid.ToString();
                sr.Issend = "0";
                if (this.xingming.Text == "" || this.xingming.Text == null)
                {
                    Message.Alertback("姓名不能为空！");
                }
                else
                {
                    xingmingtxt = this.xingming.Text;
                    sr.Name = xingmingtxt;
                }
                if (this.dianhua.Text == "" || this.dianhua.Text == null)
                {
                    Message.Alertback("电话不能为空！");
                }
                else
                {
                    if (this.dianhua.Text.Length != 11)
                    {
                        Message.Alertback("请输入有效的电话！");
                    }
                    else
                    {
                        dianhuatxt = this.dianhua.Text;
                        sr.Phone = dianhuatxt;
                    }

                }
                if (this.email.Text == "" || this.email.Text == null)
                {
                    Message.Alertback("邮箱不能为空！");
                }
                else
                {
                    int i = this.email.Text.IndexOf("@");
                    if (i == -1)
                    {
                        Message.Alertback("请输入有效的邮箱！");
                    }
                    else
                    {
                        emailtxt = this.email.Text;
                        sr.Email = emailtxt;
                    }
                }
                if (this.sheng.SelectedValue == "0")
                {
                    Message.Alertback("请选择省！");
                }
                else
                {
                    shengtxt = this.sheng.SelectedItem.Text;
                    sr.Province = shengtxt;
                }
                if (this.city.SelectedValue == "0")
                {
                    Message.Alertback("请选择市！");
                }
                else
                {
                    citytxt = this.city.SelectedItem.Text;
                    sr.City = citytxt;
                }
                if (this.quyu.SelectedValue == "0")
                {
                    Message.Alertback("请选择区域！");
                }
                else
                {
                    quyutxt = this.quyu.SelectedItem.Text;
                    sr.Area = quyutxt;
                }
                if (this.dressdesc.Text == "" || this.dressdesc.Text == null)
                {
                    Message.Alertback("具体地址不能为空！");
                }
                else
                {
                    adressdesctxt = this.dressdesc.Text;
                    sr.Adressdesc = adressdesctxt;
                }
                sr.RoomType = this.roomtype.SelectedItem.Text;
                sr.OftenServer = this.richangserver.SelectedItem.Text;
                sr.DuoTaoFangYuan = this.duotaofangyuan.SelectedItem.Text;
                sr.RoomNum = int.Parse(this.fangyuannum.SelectedItem.Text);
                sr.FaPiao = this.fapiao.SelectedItem.Text;
                sr.JiaGe = this.jiage.Text;
                sr.IsTeJia = "0";
                sr.IsTeSe = "0";
                emailbody += "亲爱的房东，";
                emailbody += "您好，欢迎您加入“爱日租”的大家庭，参与最时尚的旅游互助租房生活。 ";
                emailbody += "在“爱日租”：";
                emailbody += "您可以将闲置的房间发布在网络上，租给异地来的旅客，轻松做房东！";
                emailbody += "为了确保您在爱日租能够正确的发布房间，以吸引最多的租客预定，您仍需提供以下资料： ";
                emailbody += "* 房间标题：5-25字，建议体现出房间特色";
                emailbody += "* 房间描述：100-300字，建议包含房间简介及周围环境情况";
                emailbody += "* 房间号码：提供清晰的房间号码可确保您能与租客沟通时对应正确的房间";
                emailbody += "* 房间照片：为提升您房间的成功预订率，建议您提供至少5张清晰图片";
                emailbody += "* 银行账号：为确保您能及时无误的收到房租费，您需准确提供账户名，账号，具体开户行 ";
                emailbody += "请将以上资料提供到HOST@AIRIZU.COM，我们的客服经理也会与您取得联系，协助您顺利发布房间。";
                emailbody += "非常感谢您对我们网站的支持，预祝您在爱日租网站上万事如意，轻松赚取属于您的一桶金！";
                emailbody += "若有任何问题，可随时与我们联系：400-696-6656";
                emailbody += "谢谢！";
                emailbody += "爱日租团队";
                emailtitle = "欢迎您加入爱日租大家庭";
                int m = 0;
                m = Main.CreateInstance().InsertInfo(sr);
                if (m > 0)
                {

                    Message.Alert("我们已经您发布的房间信息，稍后我们的客服经理会与您联系，谢谢您的支持！");
                    try
                    {
                        Main.SendEmail("服务器端邮件账号", emailtxt, emailtitle, emailbody);
                    }
                    catch (Exception)
                    {
                    }
                    Message.Alerturl(HttpContext.Current.Request.Url.PathAndQuery);

                }
                else
                {
                    Message.Alertback("发布信息中出错！");
                }
            }

        }

    }
}
